<!DOCTYPE html>
<html lang="en">

<head>
  <title>three.js webgl - geometry - cube</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
  <link type="text/css" rel="stylesheet" href="https://threejs.org/examples/main.css" />
  <style>
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <script type="importmap">
      {
        "imports": {
          "three": "https://threejs.org/build/three.module.js",
          "three/addons/": "https://threejs.org/examples/jsm/"
        }
      }
    </script>

  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/addons/controls/OrbitControls.js";
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    const controls = new OrbitControls(camera, renderer.domElement);

    // 创建音频上下文
    const audioCtx = new window.AudioContext;
    // 加载音频文件并播放
    const audioLoader = new THREE.AudioLoader();
    audioLoader.load('https://threejs.org/examples/sounds/358232_j_s_song.mp3', function (buffer) {
      const audioSource = audioCtx.createBufferSource();
      audioSource.buffer = buffer;
      audioSource.connect(audioCtx.destination);
      audioSource.start();
    });

    // 将音频监听器添加到相机
    const listener = new THREE.AudioListener();
    camera.add(listener);

    // 创建一个声音对象并设置其在场景中的位置
    const sound = new THREE.Audio(listener);
    sound.position.set(5, 0, 0);

    // 动画循环
    function animate() {
      requestAnimationFrame(animate);
      controls.update();
      renderer.render(scene, camera);
    }

    animate();

  </script>
</body>

</html>